clear all
set more off
capture log close
log using "$output/figureA6.log", replace

version 16	

use "$data/ctctaxsim_mar0kid1.dta", clear
append using "$data/ctctaxsim_mar0kid2.dta"
append using "$data/ctctaxsim_mar0kid3.dta"
tab depx

sum fiitax earnings fedtaxlessctc v25 totctc
replace earnings=earnings*1000
replace ctc=ctc*1000
replace v25=v25*1000
replace totctc=totctc*1000
sum fiitax earnings fedtaxlessctc v25 totctc

gen avgtaxrate=fiitax/earnings
gen avgtaxrate2021=fedtaxlessctc/earnings	/* Don't include new CTC */

gen avgtaxeitc=-v25/earnings
gen avgtaxctc=-totctc/earnings
gen avgtaxctc2021=0

gen avgtaxeitcctc=avgtaxeitc+avgtaxctc

gen avginctax=avgtaxrate- avgtaxeitcctc

bysort depx: sum avgtax*

tab year avginctax if earnings==40000 & depx==1
replace avginctax=avginctax*.04/.037875 if year==2017
tab year avginctax if earnings==40000 & depx==1

bysort earnings depx: egen minavgtax=min(avgtaxrate)
gen difavgtax2=avgtaxrate2021-minavgtax
sum difavgtax2

bysort earnings depx: egen maxavgtax=max(avgtaxrate)
gen difavgtax=avgtaxrate2021-maxavgtax
sum difavgtax

gen difavgtax3=minavgtax-maxavgtax
drop if year==2017
foreach var in difavgtax2 difavgtax difavgtax3{
	replace `var'=-`var'
}

twoway line difavgtax2 earnings if year==2018 & depx==1, lp(-) pstyle(p1) || line difavgtax2 earnings if year==2018 & depx==2, lp(_) pstyle(p2) || line difavgtax2 earnings if year==2018 & depx==3, pstyle(p1) graphregion(color(white)) bgcolor(white) leg(order(1 "1 Kid" 2 "2 Kids" 3 "3 Kids") c(1) pos(5) ring(0)) ytitle("Change in Return to Work") xtitle("Earnings" "{bf:Panel A: 2021 vs 2020}") 
graph save "$output/avgtax2unmar.gph", replace


twoway line difavgtax earnings if year==2018 & depx==1, lp(-) pstyle(p1) || line difavgtax earnings if year==2018 & depx==2, lp(_) pstyle(p2) || line difavgtax earnings if year==2018 & depx==3, pstyle(p1) graphregion(color(white)) bgcolor(white) leg(order(1 "1 Kid" 2 "2 Kids" 3 "3 Kids") c(1) pos(5) ring(0)) ytitle("Change in Return to Work") xtitle("Earnings" "{bf:Panel B: 2021 vs 2017}") 
graph save "$output/avgtaxunmar.gph", replace


twoway line difavgtax3 earnings if year==2018 & depx==1, lp(-) pstyle(p1) || line difavgtax3 earnings if year==2018 & depx==2, lp(_) pstyle(p2) || line difavgtax3 earnings if year==2018 & depx==3, pstyle(p1) graphregion(color(white)) bgcolor(white) leg(order(1 "1 Kid" 2 "2 Kids" 3 "3 Kids") c(1) pos(2) ring(0)) ytitle("Change in Return to Work") xtitle("Earnings" "{bf:Panel C: 2020 vs 2017}")  
graph save "$output/avgtax3unmar.gph", replace


graph combine "$output/avgtax2unmar.gph" "$output/avgtaxunmar.gph" "$output/avgtax3unmar.gph",graphregion(color(white)) 
graph export "$output/figureA6.png", replace

erase "$output/avgtax2unmar.gph"
erase "$output/avgtaxunmar.gph"
erase "$output/avgtax3unmar.gph"

log close